package logic

import (
	"context"
	"strings"

	"book/service/sstwords/cmd/rpc/internal/svc"
	"book/service/sstwords/cmd/rpc/sstwords"

	"github.com/tal-tech/go-zero/core/logx"
	"github.com/tal-tech/go-zero/core/stringx"
)

type GetSstwordsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetSstwordsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSstwordsLogic {
	return &GetSstwordsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetSstwordsLogic) GetSstwords(in *sstwords.Request) (*sstwords.Response, error) {

	// 词库 - 从数据库中取数据
	var sstwords_arr = []string{
		// "演员",
	}

	rows, err := l.svcCtx.SstWordsModel.FindAllList()
	if err != nil {
		return nil, err
	}

	for _, v := range rows {
		sstwords_arr = append(sstwords_arr, v.Title)
	}

	filter := stringx.NewTrie(sstwords_arr)
	keywords := filter.FindKeywords(in.Keywords)

	logx.Infof("keywords: %v", keywords)

	return &sstwords.Response{
		Title: strings.Join(keywords, ","),
	}, nil
}
